package com.codingxyz.simple.ThreadSafe;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadSafeDemo {


    public static void main(String[] args) {
        ThreadSafeDemo threadSafeDemo = new ThreadSafeDemo();
        threadSafeDemo.teestArrayList();
    }


    public void teestArrayList(){

        CountDownLatch countDownLatch = new CountDownLatch(10);

        //List<String> list = Collections.synchronizedList(new ArrayList<>());
        List<String> list = new ArrayList<>(10);

        for(int i =0;i<10;i++){

            Executor threadPool = new ThreadPoolExecutor(
                    5,
                    10,
                    1L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(10),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.DiscardPolicy()
            );

            int finalI = i;
            threadPool.execute(()->{

                try {
                    TimeUnit.SECONDS.sleep(1);
                    String x = "aaa";
                    String  y = ",";
                    String z = "bbbb";
                    String d= x+y+z;
                    list.add(d);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    System.out.println("执行线程 ： "+ finalI+" , "+countDownLatch.getCount());
                    countDownLatch.countDown();
                }

            });
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("##################执行结束#####################"+countDownLatch.getCount()+"list : "+list.size());


    }

}
